﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 电脑的控制
/// </summary>
public class ComputerController : CharacterBase {

    /// <summary>
    /// 身份
    /// </summary>
    private Identity identity;
    public CharacterUI characterUI;

    public Identity Identity
    {
        get
        {
            return identity;
        }

        set
        {
            identity = value;
            characterUI.SetIdentity(value);
        }
    }
    public CanvasGroup txt_Pass;
    /// <summary>
    /// 电脑自动出牌的脚本对象
    /// </summary>
    public ComputerAI computerAI;

    /// <summary>
    /// 电脑出牌的类型
    /// </summary>
    public CardType currentCardType { get { return computerAI.currentCardType; } }
    /// <summary>
    /// 电脑当时选的牌
    /// </summary>
    public List<Card> SelectCards { get { return computerAI.SelectCards; } }
    /// <summary>
    /// 添加卡牌
    /// </summary>
    /// <param name="card"></param>
    public override void AddCard(Card card, bool isSelected)
    {
        base.AddCard(card, isSelected);
       
        characterUI.SetRemain(CardCount);
    }
    /// <summary>
    /// 发牌
    /// </summary>
    /// <param name="card"></param>
    public override Card DealCard()
    {
        Card card = base.DealCard();
        characterUI.SetRemain(CardCount);
        return card;
    }
    /// <summary>
    /// 排序
    /// </summary>
    /// <param name="asc"></param>
    public override void Sort(bool asc)
    {
        base.Sort(asc);
    }
    /// <summary>
    /// 电脑自动出牌
    /// </summary>
    public bool ComputerSmaterPlayCard(CardType cardType, int weight, int length, bool isBiggest)
    {
        computerAI.SmartSelectCards(CardList, cardType, weight, length, isBiggest);
        if (SelectCards.Count != 0)
        {
            //出牌成功
            DestroyCards();
            return true;
        }
        else
        {
            //没成功就pass
            ShowPass();
            return false;
        }
    }
    /// <summary>
    /// 删除牌
    /// </summary>
    public void DestroyCards()
    {
        CardUI[] cardUIs = transform.Find("CreatePoint").GetComponentsInChildren<CardUI>();
        for (int i = 0; i < SelectCards.Count; i++)
        {
            for (int j = 0; j < cardUIs.Length; j++)
            {
                if (cardUIs[j]._Card == SelectCards[i])
                {
                    cardUIs[j].Destroy();
                    CardList.Remove(SelectCards[i]);//将选中的牌从手中移除掉
                }
            }
        }
        SortCardUI(CardList);
        characterUI.SetRemain(CardCount);
    }
    /// <summary>
    /// 机器人要不起 pass
    /// </summary>
    public void ShowPass()
    {
        txt_Pass.alpha = 1f;
        StartCoroutine(PassAnim());
    }
    /// <summary>
    /// Pass 显示动画
    /// </summary>
    /// <returns></returns>
    IEnumerator PassAnim()
    {
        //while (true)
        //{
        //    txt_Pass.alpha = Mathf.Lerp(txt_Pass.alpha, 0, 0.5f);
        //    yield return new WaitForSeconds(0.2f);
        //    if (Mathf.Abs(1- txt_Pass.alpha) >= 0.01f)
        //        break;
        //}
        float time = 1f;
        while (time >= 0f)
        {
            yield return new WaitForSeconds(0.2f);
            txt_Pass.alpha -= 0.2f;
            time -= 0.2f;
        }
    }
    /// <summary>
    /// 更新分数
    /// </summary>
    /// <param name="integration"></param>
    public void ShowIntagration(int integration)
    {
        characterUI.SetIntegration(integration);

    }
}
